package com.musiklegal;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Artist extends ListActivity
{
	ProgressDialog dialog;
	Handler h;
	final String []abc={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z"};
	protected Dialog onCreateDialog(int id) {
        switch (id) {
              case 0: {
                    dialog = new ProgressDialog(this);
                    dialog.setMessage("Loading...");
                    dialog.setIndeterminate(true);
                    dialog.setCancelable(true);
                    return dialog;                   
              }
        }
        return null;
  }
	public void onCreate(Bundle savedInstanceState) 
	{
	  super.onCreate(savedInstanceState);
	  h= new Handler();
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.abc, abc));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener()
	  {

		public void onItemClick(AdapterView<?> arg0, View arg1, final int pos,
				long arg3) {
			showDialog(0);
			new Thread(new Runnable(){

				public void run() {
					h.post(new Runnable(){

						public void run() {
							
							if(MusikLegal.internet(getApplicationContext())){
				      			 try{
				      		        	
				      					HttpClient mlist = new DefaultHttpClient(); 
				      					HttpGet get = new HttpGet("http://secure.musiklegal.com/?action=artist&method=get_artist_by_letter"+"&api_key="+Browseby.apikey+"&letter="+abc[pos]+"&limit=5000");
				      					HttpResponse mresp = mlist.execute(get);

				      					HttpEntity merclist = mresp.getEntity();
				      					SAXParserFactory spf = SAXParserFactory.newInstance();
				      					SAXParser sp = spf.newSAXParser();
				      					XMLReader xr = sp.getXMLReader();
				      					 
				      					BrowsePar xmlr = new BrowsePar();
				      					xr.setContentHandler(xmlr);
				      					xr.parse(retrieveInputStream(merclist)); 
				      					
				      					Browse bw = xmlr.getBrowse();
				      					ArtistList.aname=bw.getName().split("fachr1");
				       					ArtistList.id=bw.getId().split("fachr1");
				      				
				      					startActivity(new Intent(getApplicationContext(),ArtistList.class));
				      	        		removeDialog(0);
				      					 
				      			 	}
				      				 catch (Exception e) {
				      					 
				      			        }
				      			 }
							
						}});
					
				}}).start();
			
			
			
		}
	    
	  });
	}
	private InputSource retrieveInputStream(HttpEntity httpEntity) {
        InputSource insrc = null;
        try {
              insrc = new InputSource(httpEntity.getContent());
        } catch (Exception e) {
        }
        return insrc;
  }	
}
